package com.example.myapplication2;

import android.view.View;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.room.Room;
import com.example.myapplication2.Dao.BookDao;
import com.example.myapplication2.POJO.Book;
import com.example.myapplication2.dataBase.BookDataBase;

import java.util.List;

public class RoomBookActivity extends AppCompatActivity implements View.OnClickListener {

    private BookDataBase bookDataBase;
    private BookDao bookDao;
    private EditText bookName;
    private EditText price;
    private EditText id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_room_book);

        //构建实例
        bookDataBase= Room.databaseBuilder(this,BookDataBase.class,"book")
                //允许主线程运行
                .allowMainThreadQueries()
                //允许数据库迁移,默认迁移的时候删除老版本数据表,重新创建数据表
                .addMigrations()
                .build();
        bookDao = bookDataBase.bookDao();
        //设置监听
        findViewById(R.id.saveBook).setOnClickListener(this);
        findViewById(R.id.updateBook).setOnClickListener(this);
        findViewById(R.id.deleteBook).setOnClickListener(this);
        findViewById(R.id.deleteBookAll).setOnClickListener(this);
        findViewById(R.id.findBookAll).setOnClickListener(this);
        findViewById(R.id.findBookByName).setOnClickListener(this);

        bookName =findViewById(R.id.nameEdit5);
        price = findViewById(R.id.priceEdit5);
        id = findViewById(R.id.IdEdit5);
    }

    @Override
    public void onClick(View view) {
        Book book=null;
        switch (view.getId()){
            case R.id.saveBook:
                book = new Book();
                book.setBookName(bookName.getText().toString());
                book.setPrice(Float.parseFloat(price.getText().toString()));
                bookDao.insert(book);
                break;
            case R.id.updateBook:
                book = new Book();
                book.setBookName(bookName.getText().toString());
                book.setPrice(Float.parseFloat(price.getText().toString()));
                book.setId(Long.valueOf(id.getText().toString()));
                bookDao.update(book);
                break;
            case R.id.deleteBook:
                book = new Book();
                book.setId(Long.valueOf(id.getText().toString()));
                bookDao.delete(book);
                break;
            case R.id.deleteBookAll:
                bookDao.deleteAll();
                break;
            case R.id.findBookAll:
                List<Book> books = bookDao.queryAll();
                for (Book book1 : books) {
                    System.out.println(book1);
                }
                break;
            case R.id.findBookByName:
                id.setText("");
                price.setText("");
                Book book1 = bookDao.queryByName(bookName.getText().toString());
                if (book1!=null){
                    bookName.setText("");
                    id.setText(book1.getId()+"");
                    price.setText(book1.getPrice()+"");
                    bookName.setText(book1.getBookName());
                }
                break;
        }
    }
}